﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace TheOliver.Controls
{
    public class DragBehavior : Behavior<UIElement>
    {
        private bool _isDragging = false;
        //public bool IsDragging
        //{
        //    get { return _isDragging; }
        //    set { _isDragging = value; }
        //}

        private UIElement _attachedElement;
        private UserControl _parent;

        Point lastPosition;
        TranslateTransform translatePosition;

        protected override void OnAttached()
        {
            _attachedElement = this.AssociatedObject;
            _parent = Application.Current.RootVisual as UserControl;

            _attachedElement.MouseLeftButtonDown += new MouseButtonEventHandler(StartDragging);
            _attachedElement.MouseLeftButtonUp += new MouseButtonEventHandler(MouseIsUp);
            _attachedElement.MouseMove += new MouseEventHandler(MouseIsMoving);
        }

        void MouseIsMoving(object sender, MouseEventArgs e)
        {
            if (_isDragging)
            {
                Point currentPosition = e.GetPosition(_parent);

                double dX = currentPosition.X - lastPosition.X;
                double dY = currentPosition.Y - lastPosition.Y;

                this.lastPosition = currentPosition;

                Transform oldTransform = _attachedElement.RenderTransform;
                TransformGroup rt = new TransformGroup();
                TranslateTransform newPos = new TranslateTransform();
                newPos.X = dX;
                newPos.Y = dY;

                translatePosition = newPos;
                if (oldTransform != null)
                {
                    if (oldTransform is TransformGroup)
                    {
                        foreach (var t in (oldTransform as TransformGroup).Children)
                        {
                            rt.Children.Add(t);
                        }
                    }
                    else
                    {
                        rt.Children.Add(oldTransform);
                    }
                }
                rt.Children.Add(newPos);

                MatrixTransform mt = new MatrixTransform();
                //mt.Matrix = rt.Value;
                rt.Children.Add(mt);

                //_attachedElement.RenderTransform = mt;
                _attachedElement.RenderTransform = rt;
            }
        }

        void MouseIsUp(object sender, MouseButtonEventArgs e)
        {
            _isDragging = false;
            
            _attachedElement.ReleaseMouseCapture();
        }

        public void StartDragging(object sender, MouseButtonEventArgs e)
        {
            _isDragging = true;
            lastPosition = e.GetPosition(_parent);
            _attachedElement.CaptureMouse();
        }
    }
}

